package com.bodu.core.util;

import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
import lombok.Data;

import java.util.Locale;

/**
 * 手机号码 归属地
 *
 * @author Administrator
 */
public class PhoneUtil {

    /**
     * 直辖市
     */
    private final static String[] MUNICIPALITY = {"北京市", "天津市", "上海市", "重庆市"};

    /**
     * 自治区
     */
    private final static String[] AUTONOMOUS_REGION = {"新疆", "内蒙古", "西藏", "宁夏", "广西"};

    private static final PhoneNumberUtil PHONE_NUMBER_UTIL = PhoneNumberUtil.getInstance();

    /**
     * 提供与电话号码相关的运营商信息
     */
    private static final PhoneNumberToCarrierMapper CARRIER_MAPPER = PhoneNumberToCarrierMapper.getInstance();

    /**
     * 提供与电话号码有关的地理信息
     */
    private static final PhoneNumberOfflineGeocoder GEO_CODER = PhoneNumberOfflineGeocoder.getInstance();

    /**
     * 中国大陆区区号
     */
    private final static int COUNTRY_CODE = 86;

    /**
     * 根据手机号 判断手机号是否有效
     *
     * @param phoneNumber 手机号码
     * @return true-有效 false-无效
     */
    public static boolean checkPhoneNumber(String phoneNumber) {
        long phone = Long.parseLong(phoneNumber);
        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        return PHONE_NUMBER_UTIL.isValidNumber(pn);
    }

    /**
     * 根据手机号 判断手机运营商
     *
     * @param phoneNumber 手机号码
     * @return 如：广东省广州市移动
     */
    public static String getCarrier(String phoneNumber) {
        long phone = Long.parseLong(phoneNumber);
        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        // 返回结果只有英文，自己转成成中文
        String carrierEn = CARRIER_MAPPER.getNameForNumber(pn, Locale.ENGLISH);
        String carrierZh = "";
        switch (carrierEn) {
            case "China Mobile":
                carrierZh += "移动";
                break;
            case "China Unicom":
                carrierZh += "联通";
                break;
            case "China Telecom":
                carrierZh += "电信";
                break;
            default:
                break;
        }
        return carrierZh;
    }

    /**
     * 根据手机号 获取手机归属地
     *
     * @param phoneNumber 手机号码
     * @return 如：广东省广州市
     */
    public static String getGeo(String phoneNumber) {
        long phone = Long.parseLong(phoneNumber);
        Phonenumber.PhoneNumber pn = new Phonenumber.PhoneNumber();
        pn.setCountryCode(COUNTRY_CODE);
        pn.setNationalNumber(phone);
        return GEO_CODER.getDescriptionForNumber(pn, Locale.CHINESE);
    }

    /**
     * 根据手机号 获取手机信息模型
     *
     * <pre>
     * 若返回值为null，则说明该号码无效
     * </pre>
     *
     * @param phoneNumber 手机号码
     * @return 手机信息模型PhoneModel
     */
    public static PhoneModel getPhoneModel(String phoneNumber) {
        if (checkPhoneNumber(phoneNumber)) {
            String geo = getGeo(phoneNumber);
            PhoneModel phoneModel = new PhoneModel();
            String carrier = getCarrier(phoneNumber);
            phoneModel.setCarrier(carrier);
            // 直辖市
            for (String val : MUNICIPALITY) {
                if (geo.equals(val)) {
                    phoneModel.setProvinceName(val.replace("市", ""));
                    phoneModel.setCityName(val);
                    return phoneModel;
                }
            }
            // 自治区
            for (String val : AUTONOMOUS_REGION) {
                if (geo.startsWith(val)) {
                    phoneModel.setProvinceName(val);
                    phoneModel.setCityName(geo.replace(val, ""));
                    return phoneModel;
                }
            }

            int resultAreaLengthEq2 = 2;
            // 其它
            String[] splitArr = geo.split("省");
            if (splitArr.length == resultAreaLengthEq2) {
                phoneModel.setProvinceName(splitArr[0]);
                phoneModel.setCityName(splitArr[1]);
                return phoneModel;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String[] phones = new String[]{"15956772732"};
        for (String p : phones) {
            try {
                PhoneModel phonemodel = getPhoneModel(p);
                if (phonemodel != null) {
                    System.out.println(phonemodel.getProvinceName());
                    System.out.println(phonemodel.getCityName());
                    System.out.println(phonemodel.getCarrier());
                    System.out.println(p + "--------" + phonemodel.getProvinceName() + phonemodel.getCityName());
                } else {
                    System.out.println(p + "--------");
                }
            } catch (Exception e) {
                System.out.println(p + "--------");
            }
        }
    }

    @Data
    public static class PhoneModel {
        /**
         * 省份名称
         */
        private String provinceName;

        /**
         * 城市名称
         */
        private String cityName;

        /**
         * 运营商：移动/电信/联通
         */
        private String carrier;
    }

}
